package com.suliao.web.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.suliao.common.base.PageFactory;
import com.suliao.common.base.Result;
import com.suliao.common.base.UserContext;
import com.suliao.common.util.SessionUtil;
import com.suliao.home.entity.content.ApplicationDO;
import com.suliao.home.entity.content.ApplicationTypeDO;
import com.suliao.home.entity.content.LabelDO;
import com.suliao.home.service.content.ApplicationService;
import com.suliao.home.service.content.ApplicationTypeService;
import com.suliao.home.service.content.LabelService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：LiuDeXi
 * @date ：Created in 2020/12/14 18:05
 * @description：
 * @version: 1.0
 */
@Controller
@RequestMapping("/induApp")
public class InduAppController {

    @Reference
    private LabelService labelService;

    @Reference
    private ApplicationTypeService applicationTypeService;

    @Reference
    private ApplicationService applicationService;

    /**
     * 共享工具类
     */
    @Autowired
    private SessionUtil sessionUtil;

    /**
     * 获取产业应用页面的初始化信息
     * @return
     */
    @GetMapping(value = "/induAppStart")
    public ModelAndView induAppAll(HttpServletRequest request) {
        ModelAndView mv = null;
        try {
            mv = new ModelAndView("/induApp/induApplication");

            // 获取登录人信息
            UserContext user = sessionUtil.getUserContext();

            //用于首页样式选中样式回显
            String backName = "产业应用";
            mv.addObject("backName", backName);
            mv.addObject("exhibitionName", "产业应用");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mv;
    }



    /**
     * 产业应用点击量+1
     * @return
     */
    @PostMapping("/updateClickRate")
    @ResponseBody
    public Result updateClickRate(Integer id) {
        Result result = Result.ok();
        try {
            int count = applicationService.updateClickRate(id);
            if (count == 0) {
                result = Result.error("产业应用点击量+1失败！");
            }
        } catch (Exception e) {
            result = Result.error("服务器异常！");
            e.printStackTrace();
        }
        return result;
    }

    /**
     *  产业应用页面初始化数据
     * @return
     */
    @GetMapping("/initialize")
    @ResponseBody
    public Result getInitialize(){
        Result result = Result.ok();
        //获取方案类别信息
        List<ApplicationTypeDO> applicationTypeDOList = null;
        //获取标签信息
        List<LabelDO> labelDOList = null;
        //获取应用方案信息
        Page<ApplicationDO> applicationDOPage = null;
        //返回
        Map<String,Object> map = null;
        try {
            applicationTypeDOList = applicationTypeService.getApplicationTypeList();
            labelDOList = labelService.getLabelDOList();
            applicationDOPage = applicationService.getApplicationList(PageFactory.createPageInfo(1, 10), null, null, "0", null);
            map = new HashMap<>();
            map.put("applicationTypeDOList",applicationTypeDOList);
            map.put("labelDOList",labelDOList);
            map.put("applicationDOPage",applicationDOPage);
            result.setData(map);
        }catch (Exception e){
            Result.error("服务器异常！");
            e.printStackTrace();
        }
        return result;
    }
    /**
     * 产业应用页面 查询 带分页
     * @param schemeCategory       方案类别
     * @param labelName            标签
     * @param newestOrPopularity   最新/人气
     * @param searchContent        搜索内容
     * @param pageNum              页数
     * @return
     */
    @GetMapping(value = "/getInduAppStart")
    @ResponseBody
    public Result getInduAppStart(String schemeCategory,
                                  String labelName,
                                  String newestOrPopularity,
                                  String searchContent,
                                  String pageNum) {
        Result result = Result.ok();
        Page<ApplicationDO> page = null;
        try{
            if(pageNum==null){
                pageNum = "1";
            }
            page = applicationService.getApplicationList(PageFactory.createPageInfo(Integer.parseInt(pageNum), 10), schemeCategory, labelName, newestOrPopularity, searchContent);
            result.setData(page.getRecords());
        } catch (Exception e){
            Result.error("服务器异常！");
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 获取产业应用详情页展示信息
     * @return
     */
    @GetMapping(value = "/applist")
    public ModelAndView getInduAppList(Integer id, HttpServletRequest request)  {
        ModelAndView mv = null;
        try {
            mv = new ModelAndView("/induApp/induAppLicationDetail");
            // 根据ID获取当前产业应用详情信息
            ApplicationDO applicationDO = applicationService.getApplicationDO(id);
            mv.addObject("induAppList",applicationDO);
            //用于首页样式选中样式回显
            mv.addObject("backName","产业应用");
            mv.addObject("exhibitionName","产业应用");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mv;
    }

    /**
     * 产业应用点赞
     * @return
     */
    @PostMapping("/updateCoolNum")
    @ResponseBody
    public Result updateCoolNum(Integer id,Integer number){
        Result result = Result.ok();
        try {
            int count = applicationService.updateCoolNum(id,number);
            if (count == 0) {
                result = Result.error("产业应用点赞失败！");
            }
        } catch (Exception e) {
            result = Result.error("服务器异常！");
            e.printStackTrace();
        }
        return result;
    }



}
